package com.zaicheng.cloud.netty.protocol;
/**
 @author zaicheng
 @version 1.0
 @date 2022-04-19
 @email zzc15847256473@163.com
 */

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ByteToMessageDecoder;

import java.util.List;

//@Sharable
public class ProtoStuffDecoder extends ByteToMessageDecoder {

    private  Class<?> genericClass;

    public ProtoStuffDecoder(Class<?> genericClass) {
        this.genericClass = genericClass;
    }

    @Override
    protected void decode(ChannelHandlerContext channelHandlerContext, ByteBuf byteBuf, List<Object> list) throws Exception {
        int length = byteBuf.readableBytes();
        byte[] bytes = new byte[length];
        byteBuf.readBytes(bytes);
        Object deserializer = ProtoStuffUtil.deserializer(bytes, this.genericClass);
        list.add(deserializer);
    }
}
